package com.wzh.glsc.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.glsc.product.dao.AttrAttrgroupRelationDao;
import com.wzh.glsc.product.dao.AttrDao;
import com.wzh.glsc.product.dao.AttrGroupDao;
import com.wzh.glsc.product.entity.AttrAttrgroupRelationEntity;
import com.wzh.glsc.product.entity.AttrEntity;
import com.wzh.glsc.product.entity.AttrGroupEntity;
import com.wzh.glsc.product.service.AttrGroupService;
import com.wzh.glsc.product.service.AttrService;
import com.wzh.glsc.product.vo.AttrAttrgroupRelationVo;
import com.wzh.glsc.product.vo.AttrGroupWithAttrsVo;
import com.wzh.glsc.product.vo.SkuItemVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.omg.CORBA.Object;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    private AttrDao attrDao;
    @Resource
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatalogId(Map<String, Object> params, Long catalogId) {
        if (catalogId == 0) {
            //Query是封装好的工具类，用于解析出传入的分页map:params中的分页信息便用调用
            //QueryWrapper是查询条件构造器，该泛型中<>传入需要查询的类，()中放入查询条件，不放入参数则为查询全部
            //this.page是默认生成的方法,此方法中传入两个参数：分页信息和查询条件
            //PageUtils是解析出IPage中的当前页码、总页码、每页大小、查询信息
            //IPage没搞明白具体，只知道是用于分页查询
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<AttrGroupEntity>());
            return new PageUtils(page);
        } else {
            //从map中获取查询条件
            String key = (String) params.get("key");
            //声明查询对象,此处new时加上eq 等于 select * from AttrGroupEntity where  catelog_id = ?
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catalogId);
            //(放入数据库对应的字段，查询条件)
            if (StringUtils.isNotEmpty(key)) {
                //这里是上面的sql 再加上等于  and (attr_group_name like '%key%' or sort like '%key%' or descript like '%key%')
                queryWrapper.and((obj) -> {
                    obj.like("attr_group_name", key).or().like("sort", key).or().like("descript", key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> attrIdList = attrgroupRelationEntityList.stream().map((tempObj) -> {
            return tempObj.getAttrId();
        }).collect(Collectors.toList());
        return attrDao.selectBatchIds(attrIdList);
    }

    @Transactional
    @Override
    public void removeRelation(AttrAttrgroupRelationVo[] attrAttrgroupRelationVoList) {
        List<AttrAttrgroupRelationEntity> list = Arrays.asList(attrAttrgroupRelationVoList).stream().map((tempObj) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(tempObj, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.removeRelation(list);
    }

    @Override
    public List<AttrGroupWithAttrsVo> withattrBycatelogId(Long attrGroupId) {
        List<AttrGroupEntity> attrGroupEntityList= baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", attrGroupId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos=attrGroupEntityList.stream().map((obj)->{
            AttrGroupWithAttrsVo attrGroupWithAttrsVo=new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(obj, attrGroupWithAttrsVo);
            List<AttrEntity> attrEntityList= attrService.getRelationAttrByAttrGroupId(obj.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntityList);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }


}